package com.iraeta.service.iraorder.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iraeta.dao.iraorder.IraorderwuliuDao;
import com.iraeta.dao.iraorder.IraorderwuliustepspeedDao;
import com.iraeta.entity.Iraorderwuliu;
import com.iraeta.entity.Iraorderwuliustepspeed;
import com.iraeta.entity.vo.IraorderwuliuVo;
import com.iraeta.service.iraorder.IraorderwuliuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class IraorderwuliuServiceImpl implements IraorderwuliuService {
    @Resource
    IraorderwuliuDao iraorderwuliuDao;
    @Resource
    IraorderwuliustepspeedDao iraorderwuliustepspeedDao;

    /*插入数据 同时插入物流步骤*/
    @Transactional
    @Override
    public void addIraorderwuliu(Iraorderwuliu iraorderwuliu, JSONArray stepJSONArray) {
        iraorderwuliuDao.insert(iraorderwuliu);
        for(int i=0;i<stepJSONArray.size();i++){
            JSONObject stepJSONObject = stepJSONArray.getJSONObject(i);
            Iraorderwuliustepspeed item = new Iraorderwuliustepspeed();
            item.setStepname(StrUtil.isEmpty(stepJSONObject.getString("stepname"))?"step" : stepJSONObject.getString("stepname"));
            item.setOrderwuliuid(iraorderwuliu.getId());
            item.setSortid(stepJSONObject.getIntValue("sortid"));
            this.iraorderwuliustepspeedDao.insert(item);
        }
    }

    /*修改数据 同时修改物流步骤*/
    @Transactional
    @Override
    public void updateIraorderwuliu(Iraorderwuliu iraorderwuliu,JSONArray stepJSONArray) {
        iraorderwuliuDao.updateByPrimaryKey(iraorderwuliu);
        for(int i=0;i<stepJSONArray.size();i++){
            JSONObject stepJSONObject = stepJSONArray.getJSONObject(i);
            int id = stepJSONObject.getIntValue("id");
            boolean isUpdateStep = id > 0;
            Iraorderwuliustepspeed item = isUpdateStep ? iraorderwuliustepspeedDao.selectByPrimaryKey(id) : new Iraorderwuliustepspeed();
            if(isUpdateStep){
                item.setStepname(StrUtil.isEmpty(stepJSONObject.getString("stepname"))?"step" : stepJSONObject.getString("stepname"));
                this.iraorderwuliustepspeedDao.updateByPrimaryKey(item);
            }else{
                item.setStepname(StrUtil.isEmpty(stepJSONObject.getString("stepname"))?"step" : stepJSONObject.getString("stepname"));
                item.setOrderwuliuid(iraorderwuliu.getId());
                item.setSortid(stepJSONObject.getIntValue("sortid"));
                this.iraorderwuliustepspeedDao.insert(item);
            }
        }
    }

    /*删除数据，同时删除物流步骤*/
    @Transactional
    @Override
    public void deleteIraorderwuliu(Integer id) {
        iraorderwuliuDao.deleteByPrimaryKey(id);
        iraorderwuliustepspeedDao.deleteByWuLiuId(id);
    }

    @Override
    public Iraorderwuliu getIraorderwuliu(Integer id) {
        return iraorderwuliuDao.selectByPrimaryKey(id);
    }

    @Override
    public List<Iraorderwuliu> listIraorderwuliuByOrderId(Integer orderId) {
        return iraorderwuliuDao.selectByOrderId(orderId);
    }

    @Override
    public List<IraorderwuliuVo> listIraorderwuliuByWlUserId(String wuLiuNo, Integer userId) {
        return iraorderwuliuDao.selectByWlUserId(wuLiuNo, userId);
    }
}
